# from typing_extensions import ParamSpec
from typing import KeysView

from scipy.stats.stats import brunnermunzel
from graph_cut import get_hist, get_histx, get_mask, graph_cut, mask_add_depth

import argparse
# import torch
# import torch.nn as nn
# from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
import math
import os
import json
import cv2
# torch.backends.cudnn.enabled = False
import matplotlib.pyplot as plt


parser = argparse.ArgumentParser(description='Greenhouse Image Analysis')
parser.add_argument('--image_dir',   type=str,   default='C:\\Users\\ys\\Desktop\\data_original\\', help='image_dir')
parser.add_argument('--gt_file',     type=str,   default='GroundTruth_All_388_Images.json', help='gt_file')
# parser.add_argument('--output_dir',   type=str,   default='./output_whitebg/', help='output_dir')
# parser.add_argument('--variety',   type=str,   default='Salanova', help='Aphylion, Salanova, Satine, Lugano')
parser.add_argument('--startn',   type=int,   default='0', help='Aphylion, Salanova, Satine, Lugano')
params = parser.parse_args()
print(params)

output_dir = f'./data_graphcut/'

json_all = {}
for i in range(0,400,50):
    print(i)
    json_path = os.path.join(output_dir, f'feat{i}.json')
    jsn_f = open(json_path, 'r')
    jsn = json.load(jsn_f)
    jsn_i = jsn['Measurements']
    json_all.update(jsn_i)


print(len(json_all))
results = {'Measurements': json_all}
new_jsn = json.dumps(results)
new_jsn_file = open(os.path.join(output_dir, 'feat_result.json'), 'w+')
new_jsn_file.write(new_jsn)
# a_size = 15

# keys = []
# i = 0
# for key, value in jsn_i.items():
#     if i >= params.startn and i < params.startn + 50:
#         keys.append(key)
#     i += 1


# for key in tqdm(keys, desc=str(params.startn)):
#     value=jsn_i[key]
    
#     # entropy_h, entropy_s = get_color_entropy(rgb_result)
#     # value['Entropy_h'] = float(entropy_h)
#     # value['Entropy_s'] = float(entropy_s)
    
    # json_all[key] = value
    # # json_all.update(jsn_dataset)
    # results = {'Measurements': json_all}
    # new_jsn = json.dumps(results)
    # new_jsn_file = open(os.path.join(output_dir, 'feat'+str(params.startn)+'.json'), 'w+')
    # new_jsn_file.write(new_jsn)



#     # os.makedirs(os.path.join(output_dir+'RGBImages'), exist_ok=True)
#     # os.makedirs(os.path.join(output_dir+'DepthImages'), exist_ok=True)
#     # cv2.imwrite(os.path.join(output_dir, 'RGBImages', rgb_filename), rgb_result)
#     # cv2.imwrite(os.path.join(output_dir, 'DepthImages', depth_filename), depth_result)
    
#     # r = (486, 914, 486+155+50, 914+167+100)
#     # img_ori = cv2.rectangle(img_ori, (r[1], r[0]), (r[3], r[2]), (0, 0, 0), 3)
#     # cv2.imshow('img', depth_result*200)
#     # cv2.waitKey(300)

# # cv2.imwrite(f'./output/{filename}', result)


